package com.palm.platform.controller;

import com.palm.core.data.MenuItem;
import com.palm.core.util.CollectionUtil;
import com.palm.core.MenuManager;
import com.palm.platform.domain.Role;

import com.palm.platform.domain.RolePermission;
import com.palm.platform.domain.User;
import com.palm.platform.domain.UserRole;
import com.palm.platform.service.RoleService;
import org.noear.solon.core.handle.Result;
import com.palm.core.data.PageResult;
import com.palm.core.anno.Auth;
import org.noear.solon.annotation.*;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.Page;


import java.util.List;
import java.util.Set;

/**
 * 角色管理
 * <p>
 * generated by palmboot
 **/
@Auth
@Controller
@Mapping("role")
public class RoleController {

    @Inject
    RoleService roleService;
    @Db
    SqlToyLazyDao dao;

    @Note("新增角色")
    @Mapping("add")
    public Result add(Role role) {
        dao.save(role);
        return Result.succeed();
    }

    @Note("删除角色")
    @Mapping("delete")
    @Tran
    public Result delete(List<Role> roles) {
        // dao.loadAll(roles);
        List<String> roleList = CollectionUtil.map(roles,role -> role.getSn());
        dao.deleteAll(roles);
        //删除相关权限
        dao.executeSql("delete from t_role_permission where role in (:roles)",new String[]{"roles"},new Object[]{roleList});
        //删除用户授权
        dao.executeSql("delete from t_user_role where role in (:roles)",new String[]{"roles"},new Object[]{roleList});
        return Result.succeed();
    }

    @Note("更新角色")
    @Mapping("save")
    public Result save(Role role) {
        dao.update(role,"roleType");
        return Result.succeed();
    }

    @Note("获取角色")
    @Mapping("detail")
    public Result<Role> detail(Role query) {
        Role role = dao.load(query);
        return Result.succeed(role);
    }

    @Note("获取角色列表")
    @Mapping("list")
    public Result<List<Role>> list(Role query) {
        List<Role> result = dao.findBySql("select * from t_role where #[and (role_type is null or role_type=:roleType)]", query);
        return Result.succeed(result);
    }

    @Note("保存角色权限")
    @Mapping("save_role_permission")
    @Tran
    public Result saveRolePermission(String role, List<String> permissions) {
        RolePermission query = new RolePermission();
        query.setRole(role);
        //删除已有权限
        List<RolePermission> rp = dao.findBySql("select * from t_role_permission where role=:role", query);
        dao.deleteAll(rp);

        //保存新的权限
        if (permissions.size() > 0) {
            List<RolePermission> newPermissions = CollectionUtil.map(permissions, it -> new RolePermission(role, it));
            dao.saveAll(newPermissions);
        }

        return Result.succeed();
    }

    @Note("获得全部权限树")
    @Mapping("all_permission")
    public Result<List<MenuItem>> allPermission() {
        return Result.succeed(MenuManager.allPermission());
    }

    @Note("获取角色管理列表")
    @Mapping("role_permission")
    public Result<Set<String>> rolePermissions(String role) {
        return Result.succeed(roleService.getPermissionOfRole(role));
    }
    @Note("获取含有指定用户角色用户列表")
    @Mapping("user_in_role")
    public Result<List<User>> userInRole(User query, Page page) {
        Page<User> result = dao.findPageBySql(page, "user_in_role", query);
        return PageResult.succeed(result);
    }
    @Note("获取没有对应角色用户列表")
    @Mapping("user_not_in_role")
    public Result<List<User>> userNotInRole(User query, Page page) {
        Page<User> result = dao.findPageBySql(page, "user_not_in_role", query);
        return PageResult.succeed(result);
    }
    @Note("删除角色用户")
    @Mapping("remove_users")
    public Result removeUsers(String role,List<UserRole> users) {
        users.forEach(u->u.setRole(role));
        dao.saveAll(users);
        return PageResult.succeed();
    }
    @Note("删除角色用户")
    @Mapping("add_users")
    public Result addUsers(String role,List<UserRole> users) {
        users.forEach(it->it.setRole(role));
        dao.saveAll(users);
        return PageResult.succeed();
    }
}
